package PO;


import many2one.Dept;
import many2one.Emp;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import util.HibernateUtil;

public class TestPO {
	@Test
	public void test1()
	{
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		
		Dept dept = new Dept();  //now, dept is a PO or NOT?
		dept.setName("r&d");
		
		session.save(dept);  //from now on, dept is a PO.
		
		tx.commit();
		session.close();
	}
	
	@Test
	public void test2()
	{
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		
		//dept1 == dept2 ??
		Dept dept1 = (Dept)session.load(Dept.class, 1);
		Dept dept2 = (Dept)session.load(Dept.class, 1);
		System.out.println(dept1==dept2);
		
		
		dept1.setName("ddd");
		dept2.setName("eee");
		
		//session.flush();
		
		tx.commit();
		session.close();
	}
	
	@Test
	public void test3()
	{
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		
		Emp emp = new Emp();
		emp.setId(3);
		
		session.delete(emp);
		
		tx.commit();
		session.close();
	}
	
	@Test
	public void test4()
	{
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		
		//避免如下的非常规做法！
		Dept dept = new Dept();
		dept.setId(2);
		
		//应该用下面的方式
		//Dept dept = (Dept)session.load(Dept.class, 2);
		
		Emp emp = new Emp();
		emp.setName("123");
		emp.setDept(dept);
		
		session.save(emp);
		
		tx.commit();
		session.close();
	}
}
